package android.FiltrosJava;

import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;

public class Spray extends AbstractImageBasic {
	protected int sprayAmount = 10;
	public Spray(int amount){
		setSpray(amount);
	}
	
	public void setSpray(int amount){
		sprayAmount = amount;		
	}
	
    public BufferedImage applyFilter(BufferedImage dst){
    	BufferedImage src = copiaImagem(dst);
		
		int dimX = src.getWidth();
		int dimY = src.getHeight();
		int px, py, val;
		WritableRaster raster_src = src.getRaster();
    	WritableRaster raster_dst = dst.getRaster();
    	int[] imageData = new int[raster_src.getNumBands()];    	
    	    	
		for (int y = 0; y < dimY; y++){
			for (int x = 0; x < dimX; x++){				
				val = (int) (Math.random() * sprayAmount);
				px = (int) (x + val - (2 * val * Math.random()));
				py = (int) (y + val - (2 * val * Math.random()));
				
				if ((px > -1) && (py > -1) && (px < dimX) && (py < dimY)){
					raster_src.getPixel(px,py,imageData);		    			
			    	raster_dst.setPixel(x,y,imageData);					
				}				
			}			
		}
		src = null;		
		return dst;	
    }
}